import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:jtlf/common/pref_utils.dart';
import 'package:jtlf/me/login.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AccountSecurityPage extends StatefulWidget {
  const AccountSecurityPage({super.key});

  @override
  State<AccountSecurityPage> createState() => _AccountSecurityPageState();
}

class _AccountSecurityPageState extends State<AccountSecurityPage> {
  bool _biometricEnabled = false;

  void _showLogoutDialog() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text("退出确认", style: TextStyle(fontWeight: FontWeight.bold)),
        content: const Text("确定要退出当前账号吗？"),
        actions: [
          GFButton(
            onPressed: () => Navigator.pop(context),
            text: "取消",
            type: GFButtonType.transparent,
            color: GFColors.SECONDARY,
          ),
          GFButton(
            onPressed: () async {
              // SharedPreferences pref = await SharedPreferences.getInstance();
              // logout(pref);
              SharedPreferences.getInstance().then((pref) =>
                  logout(pref)
              );
              GFToast.showToast("已退出登录", context);
              // 弹出当前页面，返回
              // Navigator.pop(context);
              // 跳转到一个新页面
              // Navigator.push(
              //   context,
              //   MaterialPageRoute(builder: (context) => LoginScreen()),
              // );
              // 替换当前路由到新页面
              Navigator.of(context).pushReplacementNamed('/login');
              // 跳转到一个新路由
              // Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginScreen()));
            },
            text: "确认退出",
            color: GFColors.DANGER,
          ),
        ],
      ),
    );
  }

  void _showAccountSwitch() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text("切换账号", style: TextStyle(fontSize: 20)),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            GFTextField(
              decoration: InputDecoration(labelText: "手机号/邮箱"),
            ),
            const SizedBox(height: 15),
            GFTextField(
              decoration: InputDecoration(labelText: "密码"),
              obscureText: true,
            ),
            const SizedBox(height: 20),
            GFButton(
              text: "登录",
              blockButton: true,
              onPressed: () {
                Navigator.pop(context);
                GFToast.showToast("切换成功", context);
              },
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: GFAppBar(
        title: const Text('账号与安全'),
        backgroundColor: GFColors.PRIMARY,
      ),
      body: ListView(
        children: [
          // 用户信息卡片
          GFCard(
            margin: const EdgeInsets.all(16),
            content: GFListTile(
              avatar: const GFAvatar(
                backgroundImage: AssetImage('assets/avatar.png'),
                size: 48,
              ),
              title: const Text('张三', style: TextStyle(fontSize: 18)),
              subTitle: const Text('zhangsan@company.com'),
              icon: GFIconButton(
                icon: const Icon(Icons.edit_outlined),
                onPressed: () {},
                type: GFButtonType.transparent,
              ),
            ),
          ),
          // 安全设置项
          _buildSecurityItem(
            title: '手机号',
            subtitle: '已绑定：138******34',
            trailing: const Icon(Icons.chevron_right, color: GFColors.SECONDARY),
          ),
          _buildSecurityItem(
            title: '登录密码',
            subtitle: '上次修改：2025-02-15',
            trailing: const Icon(Icons.chevron_right, color: GFColors.SECONDARY),
          ),
          _buildSecurityItem(
            title: '生物识别登录',
            subtitle: '指纹/面部识别',
            trailing: GFToggle(
              value: _biometricEnabled,
              onChanged: (value) => setState(() => _biometricEnabled = value!),
              enabledTrackColor: GFColors.PRIMARY,
            ),
          ),
          // 操作按钮
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
            child: Column(
              children: [
                GFButton(
                  text: "切换账号",
                  color: GFColors.PRIMARY,
                  fullWidthButton: true,
                  size: GFSize.LARGE,
                  onPressed: _showAccountSwitch,
                ),
                const SizedBox(height: 12),
                GFButton(
                  text: "退出登录",
                  color: GFColors.DANGER,
                  fullWidthButton: true,
                  size: GFSize.LARGE,
                  type: GFButtonType.outline,
                  onPressed: _showLogoutDialog,
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  // 通用设置项构建
  Widget _buildSecurityItem({
    required String title,
    String? subtitle,
    Widget? trailing, // 这个尾标变成icon了
  }) {
    return GFListTile(
      title: Text(title),
      subTitle: subtitle != null ? Text(subtitle) : null,
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      color: Colors.white,
      icon: trailing,
    );
  }
}
